package com.t2two.example.common.dataresult;

/**
 * @author 李飞
 * @date 2022/1/13
 * @desc
 * @param <T>
 */
public class DataResult<T> extends BaseResult{

    private static final String SPLIT_CHAR = ":";
    protected T data;

    public DataResult() {

    }

    public DataResult(BaseResult result) {
        super(result);
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public DataResult(T data) {
        setSuccess(true);
        setCode(ResultCode.SUCCESS.getCode());
        setMessage(ResultCode.SUCCESS.getMessage());
        this.data = data;
    }

    public boolean isSuccAndNotNull() {
        return isSuccess() && (this.data != null);
    }

    public boolean isSuccAndNull() {
        return isSuccess() && (this.data == null);
    }

    @Override
    public DataResult<T> initFailResult(ResultCode failCode) {
        return initFailResult(this, failCode);
    }

    @Override
    public DataResult<T> initFailResult(ResultCode failCode, Object... args) {
        return initFailResult(this, failCode, args);
    }

    public static <T> DataResult<T> success(T data) {
        DataResult<T> result = new DataResult<T>();
        result.setSuccess(true);
        result.setData(data);
        return result;
    }



    public static <T> DataResult<T> success(T data, String message) {
        DataResult<T> result = new DataResult<T>();
        result.setSuccess(true);
        result.setMessage(message);
        result.setData(data);
        return result;
    }

    public static <T> DataResult<T> error(String message) {
        DataResult<T> result = new DataResult<T>();
        result.setSuccess(false);
        result.setMessage(message);
        return result;
    }

    public static <T> DataResult<T> error(ErrorCode error) {
        DataResult<T> result = new DataResult<>();
        result.setSuccess(false);
        result.setErrorCode(error.getErrorCode());
        result.setMessage(error.getErrorDesc());
        return result;
    }

    public static <T> DataResult<T> error(ErrorCode error, String reasonMsg) {
        DataResult<T> result = new DataResult<>();
        result.setSuccess(false);
        result.setErrorCode(error.getErrorCode());
        result.setMessage(error.getErrorDesc() + SPLIT_CHAR + reasonMsg);
        return result;
    }

    public static <T> DataResult<T> error(ErrorCode error, String reasonMsg, Object... params) {
        DataResult<T> result = new DataResult<>();
        result.setSuccess(false);
        result.setErrorCode(error.getErrorCode());
        result.setMessage(error.getErrorDesc() + SPLIT_CHAR + format(reasonMsg,params));
        return result;
    }

    /**
     * 参数化格式化
     *
     * @param message : abc contain [%s] and [%s] and [%s]
     * @param params  log格式化参数,数组length与message参数化个数相同, 如:
     *                <code>Object[]  object=new Object[]{"xxx","xxx"}</code>
     * @return
     */
    public static String format(String message, Object... params) {
        if (params != null && params.length != 0) {
            return String.format(message, params);
        }
        return message;
    }
}
